import 'package:my_desk_flutter/api/bus_api.dart';
import 'package:my_desk_flutter/model/bus_info_model.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'bus_controller.g.dart';

@riverpod
class BusController extends _$BusController {
  late final api = ref.watch(busApiProvider);

  @override
  Future<List<BusInfoModel>> build() async {
    ref.onDispose(() {
      print("bus_controller 发生了销毁");
    });
    return [];
  }

  Future searchByName(String name) async {
    state = const AsyncLoading();
    try {
      final data = await api.searchBusByName(name: name);
      state = AsyncData(data);
    } catch (e, stack) {
      state = AsyncError(e, stack);
    }
  }
}
